/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.rie06.rmi;

import java.io.IOException;
import java.io.PrintWriter;
import java.rmi.Naming;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author clement
 */
public class live extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            Database bd = new Database();       
            bd.connect();
            
            /* TODO output your page here. You may use following sample code. */
            String server_ip = "";
            boolean isMemory = false;
            boolean isCpu = false;
            boolean isProcessus = false;
            String memoryChecked = "";
            String cpuChecked = "";
            String processusChecked = "";

            if (request.getParameter("iprmiserver") != null) {
                server_ip = request.getParameter("iprmiserver");
            }

            if (request.getParameter("memory") != null && request.getParameter("memory").equals("1")) {
                isMemory = true;
                memoryChecked = "checked";
            }

            if (request.getParameter("cpu") != null && request.getParameter("cpu").equals("1")) {
                isCpu = true;
                cpuChecked = "checked";
            }

            if (request.getParameter("processus") != null && request.getParameter("processus").equals("1")) {
                isProcessus = true;
                processusChecked = "checked";
            }

            try {
                Server obj_Server = (Server) Naming.lookup("//" + server_ip + "/Server");
                Monitoring obj_Monitoring = obj_Server.createMonitoring();
                out.println("<h2>" + server_ip + "</h2>");
                if (isMemory) {

                    out.println("<h4>Memory</h4>"
                            + "<p>"
                            + "Free : " + obj_Monitoring.getMemory().getFreeMemory()
                            + "</p>");

                    //Set DB request for data memory
                    bd.execUpdate("INSERT INTO RMIHISTO VALUES (default,'127.0.0.1','Memory','" + obj_Monitoring.getMemory().getFreeMemory() + "');");
                }

                if (isCpu) {

                    out.println("<h4>CPU</h4>"
                            + "<p>"
                            + "User Usage : " + obj_Monitoring.getCPU().getUserCPU()
                            + "</p>");

                    //Set DB request for data CPU
                    bd.execUpdate("INSERT INTO RMIHISTO VALUES (default,'127.0.0.1','CPU','" + obj_Monitoring.getCPU().getUserCPU() + "');");

                }

                if (isProcessus) {
                    out.println("<h4>Processus</h4>"
                            + "<table id=\"processtable\" class=\"table-hover\">"
                            + "<thead>"
                            + "<tr>"
                            + "<th>PID</th>"
                            + "<th>Command</th>"
                            + "<th>User</th>"
                            + "<th>%CPU</th>"
                            + "<th>%Memory</th>"
                            + "<th>Priority</th>"
                            + "</tr>"
                            + "</thead>"
                            + "<tbody>");

                    for (Proc process : obj_Monitoring.getProcessus().getListOfProcessus()) {
                        out.println("<tr>"
                                + "<td>" + process.getPid() + "</td>"
                                + "<td>" + process.getCommand() + "</td>"
                                + "<td>" + process.getUser() + "</td>"
                                + "<td>" + process.getCpu() + "</td>"
                                + "<td>" + process.getMem() + "</td>"
                                + "<td>" + process.getPriorite() + "</td>"
                                + "</tr>");
                    }

                    out.println("</tbody>"
                            + "</table>"
                            + "<link href=\"plugins/chromatable/css/style.css\" rel=\"stylesheet\">"
                            + "<script src=\"plugins/chromatable/jquery.chromatable.js\"></script>"
                            + "<script type=\"text/javascript\">"
                            + "$(\"#processtable\").chromatable({"
                            + "scrolling: \"yes\","
                            + "width: '600px'"
                            + "});"
                            + "</script>");

                    //Set DB request for data Processus
//                    List<Proc> result = obj_Monitoring.getProcessus().getListOfProcessus();
//                    bd.execUpdate("INSERT INTO RMIHISTO (default,'127.0.0.1','Processus','" + result + "');");
                    bd.saveObject(server_ip, obj_Monitoring.getProcessus().getListOfProcessus());
                   
                }
            } catch (Exception e) {
                out.println("<div class=\"alert alert-error\">"
                        + "<p>Unabled to contact this server: " + server_ip + "</p>"
                        + "</div>");
            }
            bd.close();
            
            out.println("<script type=\"text/javascript\">");
            
            out.println("$('#iprmiserver').attr('value', '"+server_ip+"');");
            if(isCpu){
                out.println("$('#checkcpu').prop('checked', true);");
            }else{
                out.println("$('#checkcpu').prop('checked', false);");
            }
            if(isMemory){
                out.println("$('#checkmemory').prop('checked', true);");
            }else{
                out.println("$('#checkmemory').prop('checked', false);");
            }
            if(isProcessus){
                out.println("$('#checkprocessus').prop('checked', true);");
            }else{
                out.println("$('#checkprocessus').prop('checked', false);");
            }
            
            out.println("</script>");
            
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
